<?php
tsLoad ( '/core/CoreLib/AbstractMysqlPage.class.php' );
class ProductPage extends AbstractMysqlPage {
	/*
	 * 取得总的行数
	 */
	protected function calTotalRows() {
		global $mysqlTools;
		$userId = $_SESSION ['userInfo'] ['userId'];
		$condition = $mysqlTools->check_input ( $this->condition, array (
				'prodName',
				'prodDesc',
				'prodUrl' 
		) );
		
		$sqlCore = "
            SELECT 
			    COUNT(*) AS totalCount
			FROM
			    `product` `p`
			        LEFT JOIN
			     `product_type` `t` ON `t`.`prodTypeId` = `p`.`prodTypeC0`
			        LEFT JOIN
			    `sys_photo` `i` ON `p`.`prodCoverId` = `i`.`photoId`
        ";
		
		$where = "
            WHERE
            	1=1  
        ";
		
		// 产品ID
		if (! empty ( $condition ['prodId'] ))
			$where .= ' AND `p`.`prodId` = ' . $condition ['prodId'];
			// 产品名称
		if (! empty ( $condition ['prodName'] ))
			$where .= ' AND `p`.`prodName` like ' . $condition ['prodName'];
			// 产品描述
		if (! empty ( $condition ['prodDesc'] ))
			$where .= ' AND `p`.`prodDesc` like ' . $condition ['prodDesc'];
			// 产品类型ID
		if (! empty ( $condition ['prodTypeC0'] ) && $condition ['prodTypeC0'] != - 1)
			$where .= ' AND `p`.`prodTypeC0` = ' . $condition ['prodTypeC0'];
			// 创建时间开始
		if (! empty ( $condition ['prodCreateTimeStart'] ))
			$where .= ' AND `p`.`prodCreateTime` >= ' . $condition ['prodCreateTimeStart'];
			// 创建时间结束
		if (! empty ( $condition ['prodCreateTimeEnd'] ))
			$where .= ' AND `p`.`prodCreateTime` <= ' . $condition ['prodCreateTimeEnd'];
		
		$sql = $sqlCore . $where;
		// 总记录数
		$totalRows = $mysqlTools->selectSql ( $sql )[0]['totalCount'];
		
		return $totalRows;
	}
	
	/*
	 * 取得数据集合
	 */
	protected function getList($sRowNo, $pageRows) {
		global $mysqlTools;
		$userId = $_SESSION ['userInfo'] ['userId'];
		$condition = $mysqlTools->check_input ( $this->condition, array (
				'prodName',
				'prodDesc',
				'prodUrl',
				'navTypeName'
		) );
		
		$sqlCore = "
            SELECT 
			    `p`.`prodId`,
			    `p`.`prodName`,
			    `p`.`prodDesc`,
			    `p`.`prodUrl`,
        		`p`.`prodCoverId`,
			    `p`.`prodSort`,
				`p`.`visitCount`,
        		`p`.`prodTypeC0`,
				`i`.`photoUrl`,
				`i`.`photoWidth`,
				`i`.`photoHeight`,
				`i`.`photoSize`
			FROM
			    `product` `p`
					LEFT JOIN
			   	`nav_type` `n` ON `p`.`navType` = `n`.id
			        LEFT JOIN
			    `product_type` `t` ON `t`.`prodTypeId` = `p`.`prodTypeC0`
			        LEFT JOIN
			    `sys_photo` `i` ON `p`.`prodCoverId` = `i`.`photoId`
        ";
		
		$where = "
            WHERE
            	1=1  
        ";
		
		// 产品ID
		if (! empty ( $condition ['prodId'] ))
			$where .= ' AND `p`.`prodId` = ' . $condition ['prodId'];
			// 产品名称
		if (! empty ( $condition ['prodName'] ))
			$where .= ' AND `p`.`prodName` like ' . $condition ['prodName'];
			// 产品描述
		if (! empty ( $condition ['prodDesc'] ))
			$where .= ' AND `p`.`prodDesc` like ' . $condition ['prodDesc'];
			//导航名称
		if (! empty ( $condition ['navTypeName'] ))
			$where .= ' AND `n`.`name` like ' . $condition ['navTypeName'];
			// 产品类型ID
		if (! empty ( $condition ['prodTypeC0'] ) && $condition ['prodTypeC0'] != - 1)
			$where .= ' AND `p`.`prodTypeC0` = ' . $condition ['prodTypeC0'];
		    //访问数大于
		if ( isset ( $condition ['visitCount'] ))
			$where .= ' AND `p`.`visitCount` > ' . $condition ['visitCount'];
			// 创建时间开始
		if (! empty ( $condition ['prodCreateTimeStart'] ))
			$where .= ' AND `p`.`prodCreateTime` >= ' . $condition ['prodCreateTimeStart'];
			// 创建时间结束
		if (! empty ( $condition ['prodCreateTimeEnd'] ))
			$where .= ' AND `p`.`prodCreateTime` <= ' . $condition ['prodCreateTimeEnd'];
			
			// 排序条件
		if (! empty ( $condition ['sortName'] ) && ! empty ( $condition ['sortType'] ))
			$order = " order by `p`.`$condition[sortName]` `$condition[sortType]`";
		else
			$order = ' ORDER BY `p`.`prodSort` ASC';
		$limit = " LIMIT $sRowNo,$pageRows";
		$sql = $sqlCore . $where . $order . $limit;
		// 查询产品分页
		$list = $mysqlTools->selectSql ( $sql );
		
		return $list;
	}
	
	/*
	 * 查询单一产品
	 */
	public function queryProductById($prodId) {
		global $mysqlTools;
		$prodId = $mysqlTools->check_input ( $prodId );
		$sql = "
            SELECT
			    `p`.`prodId`,
			    `p`.`prodName`,
			    `p`.`shopName`,
			    `p`.`shopUrl`,
			    `p`.`prodDesc`,
			    `p`.`prodUrl`,
    			`p`.`prodCoverId`,
    			`p`.`navType`,
			    `p`.`prodSort`,
    			`p`.`prodPrice`,
    			`p`.`prodNowPrice`,
    			`p`.`prodAppraise`,
    			`p`.`prodSort`,
    			`p`.`prodTypeC0`,
    			`p`.`prodState`,
    			`p`.`prodRecomLevel`,
    			`n`.`name` AS navTypeName,
    			`t`.`prodTypeName`,
				`i`.`photoUrl`,
				`i`.`photoWidth`,
				`i`.`photoHeight`,
				`i`.`photoSize`
			FROM
			    `product` `p`
			    	LEFT JOIN
			   	`nav_type` `n` ON `p`.`navType` = `n`.id
			        LEFT JOIN
			    `product_type` `t` ON `t`.`prodTypeId` = `p`.`prodTypeC0`
			        LEFT JOIN
			    `sys_photo` `i` ON `p`.`prodCoverId` = `i`.`photoId`
    		WHERE
            	`p`.`prodId` = $prodId
        ";
		return $mysqlTools->selectSql ( $sql )[0];
	}
	
	// 查询行业类型
	public function queryProductTypeTree($typeParentId) {
		global $mysqlTools;
		tsLoad('/manage/busi/CommonBusi.class.php');
		$typeParentId = $mysqlTools->check_input ( $typeParentId );
		
		$productTypeTree = $mysqlTools->selectSql ( "
    			SELECT 
					`t`.`prodTypeId`,
				    `t`.`prodTypeName`,
				    `t`.`prodTypeDesc`,
				    `t`.`prodTypeCategory`,
				    `t`.`prodTypeIndex`,
				    `t`.`prodTypeOrder`,
				    `t`.`prodTypeParentId`,
				    `t`.`prodTypeCreateTime`,
				    `t`.`prodTypeState`,
				    `t`.`prodTypeQuantity`,
				    `t`.`prodTypeIndexCode`,
				    `t`.`prodTypeIsHot`,
				    `t`.`prodTypeCateIndex`,
					`i`.`photoId`,
					`i`.`photoUrl`,
					`i`.`photoWidth`,
					`i`.`photoHeight`,
					`i`.`photoSize`
				FROM `product_type` `t`
				LEFT JOIN
			    	`sys_photo` `i` ON `t`.`prodTypePhotoId` = `i`.`photoId` 
    			WHERE
					`t`.`prodTypeState` = 1
    			ORDER BY `t`.`prodTypeOrder` ASC
    	" );
		
		$productTypeTree = CommonBusi::getTree($productTypeTree, $typeParentId, 'prodTypeId', 'prodTypeParentId', 'child');
		
		return $productTypeTree;
	}
	
	/**
	 * 添加产品
	 */
	public function addProduct($condition, $uniqid) {
		global $mysqlTools;
		// 参数处理注入SQL安全问题
		$condition = $mysqlTools->check_input ( $condition );
		$prodTypeC = $this->queryProductTypeParentsById ( array (), $condition ['prodTypeC0'] );
		// 父类依次赋值
		for($i = 4, $j = 0; $i > 0; $i --) {
			$condition ['prodTypeC' . $i] = $i <= count ( $prodTypeC ) ? $prodTypeC [$j ++] : null;
		}
		$condition = $mysqlTools->check_null ( $condition );
		$userId = $_SESSION ['userInfo'] ['userId'];
		try {
			$result = $mysqlTools->executeSql ( "
    				INSERT INTO `product`
					(	
						`prodId`,
						`prodName`,
						`prodDesc`,
						`shopName`,
						`shopUrl`,
						`prodPrice`,
						`prodNowPrice`,
						`prodAppraise`,
						`prodUrl`,
						`prodCoverId`,
						`navType`,
						`visitCount`,
						`prodSort`,
						`prodTypeC0`,
						`prodTypeC1`,
						`prodTypeC2`,
						`prodTypeC3`,
						`prodTypeC4`,
						`prodUserId`,
						`prodSiteId`,
						`prodCreateTime`,
						`prodModifyTime`,
						`prodState`,
						`prodSourceId`,
						`prodRecomLevel`
					)VALUES(
						$uniqid,
						$condition[prodName],
						$condition[prodDesc],
						$condition[shopName],
						$condition[shopUrl],
						$condition[prodPrice],
						$condition[prodNowPrice],
						$condition[prodAppraise],
						$condition[prodUrl],
						$condition[prodCoverId],
						$condition[navType],
						'0',
						$condition[prodSort],
						$condition[prodTypeC0],
						$condition[prodTypeC1],
						$condition[prodTypeC2],
						$condition[prodTypeC3],
						$condition[prodTypeC4],
						$userId,
						null,
						now(),
						null,
						1,
						null,
						$condition[prodRecomLevel]
					)
	    	" );
		} catch ( \Exception $e ) {
			return false;
		}
		return $result;
	}
	public function addProductProxy($condition) {
		global $mysqlTools;
		// 生成唯一ID
		$uniqid = getCurrentId ();
		try {
			$mysqlTools->startTransaction ();
			$prodList = $this->queryProductByTyeId ( $condition ['prodTypeC0'] );
			$result = $this->addProduct ( $condition, $uniqid );
			if (! $result)
				return false;
				// 重新排序
			$prodList = arraySort ( $prodList, $id = 'prodId', $sort = 'prodSort', array (
					'prodId' => $uniqid,
					'prodSort' => $condition ['prodSort'] 
			), 'add' );
			foreach ( $prodList as $value ) {
				$editSort = $this->updateProduct ( $value );
				if (! $editSort) {
					$mysqlTools->rollback ();
					return false;
				}
			}
			$mysqlTools->commitTransaction ();
		} catch ( \Exception $e ) {
			$mysqlTools->rollback ();
			return false;
		}
		return $result;
	}
	
	/**
	 * 修改产品
	 */
	public function updateProductProxy($condition) {
		global $mysqlTools;
		try {
			$mysqlTools->startTransaction ();
			$result = $this->updateProduct ( $condition );
			if (! $result) {
				$mysqlTools->rollback ();
				return false;
			}
			$prodList = $this->queryProductByTyeId ( $condition ['prodTypeC0'] );
			// 重新排序
			$prodList = arraySort ( $prodList, $id = 'prodId', $sort = 'prodSort', array (
					'prodId' => $condition ['prodId'],
					'prodSort' => $condition ['prodSort'] 
			), 'move' );
			foreach ( $prodList as $value ) {
				$editSort = $this->updateProduct ( $value );
				if (! $editSort) {
					$mysqlTools->rollback ();
					return false;
				}
			}
			$mysqlTools->commitTransaction ();
		} catch ( \Exception $e ) {
			$mysqlTools->rollback ();
			return false;
		}
		return $result;
	}
	
	/**
	 * 修改产品
	 */
	public function updateProduct($condition) {
		global $mysqlTools;
		// 参数处理注入SQL安全问题
		$condition = $mysqlTools->check_input ( $condition );
		$condition = $mysqlTools->check_null ( $condition );
		$sql = "UPDATE `product` SET `prodId` = `prodId`";
		if (! empty ( $condition ['prodName'] )) {
			$sql .= ",`prodName` = $condition[prodName]";
		}
		if (! empty ( $condition ['shopName'] )) {
			$sql .= ",`shopName` = $condition[shopName]";
		}
		if (! empty ( $condition ['shopUrl'] )) {
			$sql .= ",`shopUrl` = $condition[shopUrl]";
		}
		if (! empty ( $condition ['prodDesc'] )) {
			$sql .= ",`prodDesc` = $condition[prodDesc]";
		}
		if (! empty ( $condition ['prodPrice'] )) {
			$sql .= ",`prodPrice` = $condition[prodPrice]";
		}
		if (! empty ( $condition ['prodNowPrice'] )) {
			$sql .= ",`prodNowPrice` = $condition[prodNowPrice]";
		}
		if (! empty ( $condition ['prodAppraise'] )) {
			$sql .= ",`prodAppraise` = $condition[prodAppraise]";
		}
		if (! empty ( $condition ['prodUrl'] )) {
			$sql .= ",`prodUrl` = $condition[prodUrl]";
		}
		if (! empty ( $condition ['prodCoverId'] )) {
			$sql .= ",`prodCoverId` = $condition[prodCoverId]";
		}
		if (! empty ( $condition ['navType'] ) && $condition ['navType'] != -1) {
			$sql .= ",`navType` = $condition[navType]";
		}
		if (! empty ( $condition ['navType'] ) && $condition ['navType'] == -1) {
			$sql .= ",`navType` = null";
		}
		if (! empty ( $condition ['prodTypeC0'] )) {
			$sql .= ",`prodTypeC0` = $condition[prodTypeC0]";
		}
		if (isset ( $condition ['prodSort'] ) && $condition ['prodSort'] != null) {
			$sql .= ",`prodSort` = $condition[prodSort]";
		}
		if (isset ( $condition ['prodState'] ) && $condition ['prodState'] != null) {
			$sql .= ",`prodState` = $condition[prodState]";
		}
		if (isset ( $condition ['prodRecomLevel'] ) && $condition ['prodRecomLevel'] != null) {
			$sql .= ",`prodRecomLevel` = $condition[prodRecomLevel]";
		}
		
		$sql .= " where `prodId` = $condition[prodId]";
		$result = $mysqlTools->executeSql ( $sql );
		return $result;
	}
	
	// 查询行业类型
	public function queryProductTypeById($prodTypeId) {
		global $mysqlTools;
		$prodTypeId = $mysqlTools->check_input ( $prodTypeId );
		$productTypeList = $mysqlTools->selectSql ( "
    			SELECT
    			`prodTypeId`,
    			`prodTypeName`,
    			`prodTypeDesc`,
    			`prodTypeCategory`,
    			`prodTypeIndex`,
    			`prodTypeOrder`,
    			`prodTypeParentId`,
    			`prodTypeCreateTime`,
    			`prodTypeState`,
    			`prodTypeQuantity`,
    			`prodTypeIndexCode`,
    			`prodTypeIsHot`,
    			`prodTypeCateIndex`
    			FROM `product_type`
    			WHERE
    			`prodTypeId` = $prodTypeId
    			AND `prodTypeState` = 1
    			" );
		return $productTypeList [0];
	}
	
	// 查询行业类型
	public function queryProductTypeParentsById($typeArray = array(), $prodTypeId) {
		global $mysqlTools;
		array_push ( $typeArray, $prodTypeId );
		$productTypeList = $mysqlTools->selectSql ( "
    			SELECT
	    			`prodTypeParentId`
	    			FROM `product_type`
    			WHERE
    				`prodTypeId` = $prodTypeId
    			AND `prodTypeState` = 1
    	" );
		if ($productTypeList [0] ['prodTypeParentId'] != 0) {
			$typeArray = $this->queryProductTypeParentsById ( $typeArray, $productTypeList [0] ['prodTypeParentId'] );
		}
		return $typeArray;
	}
	
	// 查询行业类型排序号和ID
	public function queryNavType() {
		global $mysqlTools;
		$typeList = $mysqlTools->selectSql ( "
			SELECT 
				`id`,
			    `name`,
			    `desc`,
			    `sort`,
			    `state`,
				0 AS pid 
			FROM `nav_type`
			ORDER BY sort ASC
		" );
		return $typeList;
	}
	
	// 查询行业类型排序号和ID
	public function queryProductTypeByPId($prodTypeParentId) {
		global $mysqlTools;
		$productTypeList = $mysqlTools->selectSql ( "
				SELECT
				`prodTypeId`,
				`prodTypeOrder`
				FROM `product_type`
				WHERE
				`prodTypeParentId` = $prodTypeParentId
				ORDER BY `prodTypeOrder`
				" );
		return $productTypeList;
	}
	
	/**
	 * 添加产品类型proxy
	 */
	public function addProductTypeProxy($condition, $uniqid) {
		global $mysqlTools;
		
		try {
			$mysqlTools->startTransaction ();
			$typeList = $this->queryProductTypeByPId ( $condition ['prodTypeParentId'] );
			$result = $this->addProductType ( $condition, $uniqid );
			if (! $result) {
				$mysqlTools->rollback ();
				return false;
			}
			$typeList = arraySort ( $typeList, $id = 'prodTypeId', $sort = 'prodTypeOrder', array (
					'prodTypeId' => $uniqid,
					'prodTypeOrder' => $condition ['prodTypeOrder'] 
			), 'add' );
			foreach ( $typeList as $value ) {
				$addSort = $this->updateProductType ( $value );
				if (! $addSort) {
					$mysqlTools->rollback ();
					return false;
				}
			}
			$mysqlTools->commitTransaction ();
		} catch ( \Exception $e ) {
			$mysqlTools->rollback ();
			return false;
		}
		return $result;
	}
	
	/**
	 * 添加产品类型
	 */
	public function addProductType($condition, $uniqid) {
		global $mysqlTools;
		
		// 参数处理注入SQL安全问题
		$condition = $mysqlTools->check_input ( $condition );
		$condition = $mysqlTools->check_null ( $condition );
		try {
			$photoId = null;
			$photoId = $condition ['prodCoverId'];
			$result = $mysqlTools->executeSql ( "
					INSERT INTO `product_type`
					(
						`prodTypeId`,
						`prodTypeName`,
						`prodTypeDesc`,
						`prodTypeCategory`,
						`prodTypeOrder`,
						`prodTypeParentId`,
						`prodTypeCreateTime`,
						`prodTypeState`,
						`prodTypeQuantity`,
						`prodTypeIndexCode`,
						`prodTypeIsHot`,
						`prodTypePhotoId`
					)VALUES(
						$uniqid,
						$condition[prodTypeName],
						$condition[prodTypeDesc],
						$condition[prodTypeCategory],
						$condition[prodTypeOrder],
						$condition[prodTypeParentId],
						now(),
						1,
						0,
						null,
						$condition[prodTypeIsHot],
						$photoId
					)
			" );
		} catch ( \Exception $e ) {
			return false;
		}
		return $result;
	}
	
	/**
	 * 修改产品分类
	 */
	public function updateProductTypeProxy($condition) {
		global $mysqlTools;
		try {
			$mysqlTools->startTransaction ();
			$result = $this->updateProductType ( $condition );
			if (! $result) {
				$mysqlTools->rollback ();
				return false;
			}
			$typeList = $this->queryProductTypeByPId ( $condition ['prodTypeParentId'] );
			$typeList = arraySort ( $typeList, $id = 'prodTypeId', $sort = 'prodTypeOrder', array (
					'prodTypeId' => $condition ['prodTypeId'],
					'prodTypeOrder' => $condition ['prodTypeOrder'] 
			), 'move' );
			foreach ( $typeList as $value ) {
				$addSort = $this->updateProductType ( $value );
				if (! $addSort) {
					$mysqlTools->rollback ();
					return false;
				}
			}
			$mysqlTools->commitTransaction ();
		} catch ( \Exception $e ) {
			$mysqlTools->rollback ();
		}
		return $result;
	}
	
	/**
	 * 修改产品分类
	 */
	public function updateProductType($condition) {
		global $mysqlTools;
		// 参数处理注入SQL安全问题
		$condition = $mysqlTools->check_input ( $condition );
		$condition = $mysqlTools->check_null ( $condition );
		try {
			$sql = "UPDATE `product_type` SET `prodTypeId` = `prodTypeId`";
			if (! empty ( $condition ['prodTypeName'] )) {
				$sql .= ",`prodTypeName` = $condition[prodTypeName]";
			}
			if (! empty ( $condition ['prodTypeDesc'] )) {
				$sql .= ",`prodTypeDesc` = $condition[prodTypeDesc]";
			}
			if (! empty ( $condition ['prodTypeCategory'] )) {
				$sql .= ",`prodTypeCategory` = $condition[prodTypeCategory]";
			}
			if (! empty ( $condition ['prodTypeOrder'] )) {
				$sql .= ",`prodTypeOrder` = $condition[prodTypeOrder]";
			}
			if (! empty ( $condition ['prodTypeParentId'] )) {
				$sql .= ",`prodTypeParentId` = $condition[prodTypeParentId]";
			}
			if (! empty ( $condition ['prodCoverId'] )) {
				$sql .= ",`prodTypePhotoId` = $condition[prodCoverId]";
			}
			if (isset ( $condition ['prodTypeIsHot'] )) {
				$sql .= ",`prodTypeIsHot` = $condition[prodTypeIsHot]";
			}
			if (isset ( $condition ['prodTypeState'] )) {
				$sql .= ",`prodTypeState` = $condition[prodTypeState]";
			}
			
			$sql .= " where `prodTypeId` = $condition[prodTypeId]";
			$result = $mysqlTools->executeSql ( $sql );
			if (! $result) {
				return false;
			}
		} catch ( \Exception $e ) {
			return false;
		}
		return $result;
	}
	
	/**
	 * 删除产品
	 */
	public function delProductByIdsProxy($prodIds) {
		tsLoad ( '/manage/busi/SysPhotoBusi.class.php' );
		global $mysqlTools;
		try {
			$mysqlTools->startTransaction ();
			$result = $this->delProductByIds($prodIds);
			if(!$result){
				$mysqlTools->rollback ();
				return false;
			}
			$mysqlTools->commitTransaction ();
		} catch ( \Exception $e ) {
			$mysqlTools->rollback ();
			return false;
		}
		return $result;
	}
	
	/**
	 * 删除产品
	 */
	public function delProductByIds($prodIds, $isSort = true) {
		tsLoad ( '/manage/busi/SysPhotoBusi.class.php' );
		global $mysqlTools;
		// 参数处理注入SQL安全问题
		$list = explode ( ',', $prodIds );
		$list = $mysqlTools->check_input ( $list );
		$con = '';
		foreach ( $list as $value ) {
			$con .= $value . ',';
		}
		$con = rtrim ( $con, "," );
		$prodList = $this->queryProductsById ( $con );
		$photoIds = '';
		$prodType = array ();
		foreach ( $prodList as $key => $value ) {
			$photoIds .= $value ['prodCoverId'] . ',';
			array_push ( $prodType, $value ['prodTypeC0'] );
		}
		$photoIds = rtrim ( $photoIds, "," );
		$prodType = array_unique ( $prodType );
		try {
			$sysPhotoBusi = new SysPhotoBusi ();
			if(!empty($photoIds)){
				/* 删除图片 */
				$phoRes = $sysPhotoBusi->delPhotoByPhotoIds ( $photoIds );
				if (! $phoRes) {
					return false;
				}
			}
			/* 删除产品 */
			$result = $this->delProducts ( $prodIds );
			if (! $result) {
				return false;
			}
			/* 重新排序 */
			if ($isSort) {
				while ( ! empty ( $prodType [0] ) ) {
					$prodList = $this->queryProductByTyeId ( array_shift ( $prodType ) );
					$prodList = arraySort ( $prodList, $id = 'prodId', $sort = 'prodSort' );
					foreach ( $prodList as $value ) {
						$editSort = $this->updateProduct ( $value );
						if (! $editSort) {
							return false;
						}
					}
				}
			}
		} catch ( \Exception $e ) {
			return false;
		}
		return $result;
	}
	
	/*
	 * 根据分类查询产品ID和排序号
	 */
	public function queryProductByTyeId($prodTypeId) {
		global $mysqlTools;
		$prodId = $mysqlTools->check_input ( $prodTypeId );
		$sql = "
		SELECT
		`p`.`prodId`,
		`p`.`prodSort`
		FROM
		`product` `p`
		WHERE
		`p`.`prodTypeC0` = $prodId
		ORDER BY `p`.`prodSort`
		";
		return $mysqlTools->selectSql ( $sql );
	}
	
	// 删除产品
	public function delProducts($prodIds) {
		global $mysqlTools;
		$result = $mysqlTools->executeSql ( "
					DELETE FROM `product`
		    		WHERE `prodId` in ($prodIds)
							" );
		if (! $result) {
			return false;
		}
		return $result;
	}
	
	/*
	 * 查询多个产品
	 */
	public function queryProductsById($prodIds) {
		global $mysqlTools;
		$sql = "
		SELECT
		`p`.`prodId`,
		`p`.`prodCoverId`,
		`p`.`prodTypeC0`,
		`p`.`prodSort`
		FROM
		`product` `p`
		LEFT JOIN
		`sys_photo` `i` ON `p`.`prodCoverId` = `i`.`photoId`
		WHERE
		`p`.`prodId` in ($prodIds)
		";
		return $mysqlTools->selectSql ( $sql );
	}
	
	/**
	 * 删除产品分类
	 */
	public function delProductType($condition) {
		global $mysqlTools;
		// 参数处理注入SQL安全问题
		$condition = $mysqlTools->check_input ( $condition );
		try {
			$mysqlTools->startTransaction ();
			$result = $this->delSelfAndChildren ( $condition );
			$typeList = $this->queryProductTypeByPId ( $condition ['prodTypeParentId'] );
			$typeList = arraySort ( $typeList, $id = 'prodTypeId', $sort = 'prodTypeOrder' );
			foreach ( $typeList as $value ) {
				$delSort = $this->updateProductType ( $value );
				if (! $delSort) {
					$mysqlTools->rollback ();
					return false;
				}
			}
			if ($result)
				$mysqlTools->commitTransaction ();
			else
				$mysqlTools->rollback ();
		} catch ( \Exception $e ) {
			$mysqlTools->rollback ();
			return false;
		}
		return $result;
	}
	
	/**
	 * 递归删除产品分类
	 */
	public function delSelfAndChildren($condition) {
		global $mysqlTools;
		$result = $mysqlTools->executeSql ( "
    		DELETE FROM `product_type`
    		WHERE `prodTypeId` = $condition[prodTypeId]
    	" );
		if (! $result)
			return false;
		$prodList = $this->queryProductByTyeId ( $condition ['prodTypeId'] );
		$prodIds = '';
		foreach ( $prodList as $value ) {
			$prodIds .= $value ['prodId'] . ',';
		}
		$prodIds = rtrim ( $prodIds, "," );
		if (strlen ( $prodIds ) > 0) {
			$result = $this->delProductByIds ( $prodIds,false );
			if (! $result) {
				return false;
			}
		}
		$chlidren = $mysqlTools->selectSql ( "
    		SELECT
    			`prodTypeId`
    		FROM `product_type`
    		WHERE
    			`prodTypeParentId` = $condition[prodTypeId]
    	" );
		foreach ( $chlidren as $key => $val ) {
			$res = $this->delSelfAndChildren ( $val );
			if (! $res)
				return false;
		}
		return true;
	}
}

?>